﻿function as_toggle(as) {
    if (as[1].style.visibility == "") {
        as[1].style.visibility = "hidden";
        as[0].focus();
    }
    else {
        var offsetTop = as[0].offsetHeight;
        var offsetLeft = 0;
        var ele = as[0];
        do {
            offsetLeft += ele.offsetLeft;
            offsetTop += ele.offsetTop;
            ele = ele.offsetParent;
        } while (ele.tagName != "BODY");

        as[1].style.width = as[0].offsetWidth;
        as[1].style.left = offsetLeft;
        as[1].style.top = offsetTop;
        as[1].style.visibility = "";
        as[1].focus();
    }
}

function as_select(as) {
    as[0].value = as[1].options.item(as[1].selectedIndex).innerText;
}

function as_keyu(as) {
    if (as[1].style.visibility != "") {
        as_toggle(as);
        as[0].focus();
    }
    switch (event.keyCode) {
        case 16: // Shift
        case 18: // Alt
        case 19: // Pause/Break
        case 20: // Caps Lock
        case 33: // Page Up
        case 34: // Page Down
        case 35: // End
        case 36: // Home
        case 37: // Left Arrow
        case 39: // Right Arrow
        case 44: // Print Screen
        case 46: // Delete
        case 112: // F1
        case 113: // F2
        case 114: // F3
        case 115: // F4
        case 116: // F5
        case 117: // F6
        case 118: // F7
        case 119: // F8
        case 120: // F9
        case 121: // F10
        case 122: // F11
        case 123: // F12
            break;
        case 9: // Tab
        case 13: // Enter
        case 27: // Esc
            as_toggle(as);
            break;
        case 38: // Up Arrow
        case 40: // Down Arrow
            as[1].focus();
            break;
        case 8:  // Backspace
            if (as[0].value.length > 0) as[0].value = as[0].value.substr(0, as[0].value.length - 1);
        case 17: // Ctrl (captures Ctrl-v paste)
        // as[0] = TextBox
        // as[1] = ListBox
        // as[2] = Template
        default:
            // Test template and add template character as needed
            if ((event.keyCode != 8) && (as[2].length > as[0].value.length))
                if (as[2].charAt(as[0].value.length) != '#') as[0].value += as[2].charAt(as[0].value.length);

            // Half-split options and get near target
            var step = Math.floor(as[1].options.length / 2);
            var i = step;
            var x = "x";
            while (1) {
                // if input is larger than target
                if (as[0].value.toUpperCase() > as[1].options.item(i).innerText.substr(0, as[0].value.length).toUpperCase()) {
                    if (as[0].value.toUpperCase() < as[1].options.item(i + 1).innerText.substr(0, as[0].value.length).toUpperCase()) {
                        i = -1;
                        break;
                    }
                    if (step > 1) step = Math.floor(step / 2);
                    i += step;
                }
                // if input is smaller than target
                else if (as[0].value.toUpperCase() < as[1].options.item(i).innerText.substr(0, as[0].value.length).toUpperCase()) {
                    i -= step;
                }
                // if input is equal to target
                else {
                    if ((i > 0) && (as[0].value.toUpperCase() == as[1].options.item(i - 1).innerText.substr(0, as[0].value.length).toUpperCase()))
                        i -= step;
                    else
                        break;
                }
                // Prevent index from exceeding bounds
                if ((i < 0) || (i >= as[1].options.length)) {
                    i = -1;
                    break;
                }
            }
            // Select text
            as[1].selectedIndex = i;
            if (i > -1) {
                var start = as[0].value.length;
                as[0].value = as[1].options.item(i).innerText;
                var tr = as[0].createTextRange();
                tr.moveStart("character", start);
                tr.moveEnd("character", as[0].value.length);
                tr.select();
            }
    }
}

function as_keyd(as) {
    if( (event.keyCode == 13) || (event.keyCode == 9))
        as_toggle(as);
}
